package com.yonyou.brigade.third_parties.hcholiday.util;

import com.xiaoleilu.hutool.util.IdcardUtil;
import com.yonyou.brigade.third_parties.common.model.ThirdUser;
import com.yonyou.brigade.third_parties.hcholiday.model.HcHolidayUser;
import com.yonyou.ybzboot.starter.common.exception.CustomException;
import com.yonyou.ybzboot.uc.dto.UcUserDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 花城用户工具类
 *
 * @author kangfw5
 * @date 2019-06-18
 */
@Slf4j
public class HcHolidayUserUtil {
    private static String DATE_FORMAT1 = "yyyyMMdd";
    private static String DATE_FORMAT2 = "yyyy-MM-dd";

    public static HcHolidayUser ucUser2Hc(String tenantId, String cuicode, UcUserDto ucUserDto) {
        String passengerId = ucUserDto.getPassengerId();

        HcHolidayUser hcHolidayUser = new HcHolidayUser();
        hcHolidayUser.setUserId(ucUserDto.getUserId());
        hcHolidayUser.setTenant(tenantId);
        hcHolidayUser.setOrg(ucUserDto.getOrgPk());
        hcHolidayUser.setCuicode(cuicode);
        hcHolidayUser.setWcodeSrc(ucUserDto.getPersonCode());
        hcHolidayUser.setStatus(ucUserDto.getValid() == 1 ? "70" : "10");
        hcHolidayUser.setWname(ucUserDto.getUserName());

        hcHolidayUser.setName(ucUserDto.getUserName());

        hcHolidayUser.setRank(ucUserDto.getPostPk());
        hcHolidayUser.setMobile(ucUserDto.getPhone());
        hcHolidayUser.setEmail(ucUserDto.getEmail());

        if (!StringUtils.isEmpty(passengerId) && IdcardUtil.isValidCard(passengerId)) {
            //身份证
            hcHolidayUser.setCardtype("1");
            hcHolidayUser.setCardnum(passengerId);

            //性别处理
            int gender = IdcardUtil.getGenderByIdCard(passengerId);
            hcHolidayUser.setSex(gender == 1 ? "1" : "2");
            String birthday = IdcardUtil.getBirthByIdCard(passengerId);
            SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_FORMAT1);
            SimpleDateFormat sdf2 = new SimpleDateFormat(DATE_FORMAT2);
            try {
                hcHolidayUser.setBirthdate(sdf2.format(sdf1.parse(birthday)));
            } catch (ParseException e) {
                log.error("花城人员信息转换失败，出生日期转换出错");
            }
        } else {
            throw new CustomException("用户缺少证件信息");
        }
        //成人
        hcHolidayUser.setTravelertype("3");

        hcHolidayUser.setCreateTime(new Date());

        return hcHolidayUser;
    }

    public static List<ThirdUser> ucUser2Hc(String tenantId, String cuicode, List<UcUserDto> ucUserList) {
        List<ThirdUser> hcUserList = new ArrayList<>();
        for (UcUserDto ucUserDto : ucUserList) {
            try {
                hcUserList.add(ucUser2Hc(tenantId, cuicode, ucUserDto));
            } catch (Exception e) {
                log.error("租户{}工号{}用户缺少证件信息",tenantId, ucUserDto.getPersonCode());
            }
        }
        return hcUserList;
    }
}
